﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section10
	Inherits System.Web.UI.UserControl
	Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "EVENT FOR CONTROL"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        'Label
        Translate()
        If Not IsPostBack Then
            Me.EnableViewState = False
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidMSDSID.Value = Request.QueryString("Id")
                btnPrevious.Attributes.Add("onclick", "location.href='Section09.aspx?Id=" & hidMSDSID.Value & "'")
            End If
            CheckData()
        End If
    End Sub
    Protected Sub Translate()
        lblSection10Title.Text = getTranslate("lblSection10Title", Session("LanguageId"), lblSection10Title.Text)
        lbSection10lbStabilize.Text = getTranslate("lbSection10lbStabilize", Session("LanguageId"), lbSection10lbStabilize.Text)
        lbSection10lbReactivityr.Text = getTranslate("lbSection10lbReactivityr", Session("LanguageId"), lbSection10lbReactivityr.Text)
    End Sub
	Protected Sub btnSection01Save_Click(sender As Object, e As EventArgs) Handles btnSection01Save.Click
		Using data As New VSEntities
			Try
				Dim p As MSDSSection10
				p = (From q In data.MSDSSection10
				Where q.Section10Id = hidID.Value
					 ).FirstOrDefault()
				Dim _isNew = True
				If Not p Is Nothing Then
					_isNew = False
				Else
					p = New MSDSSection10
				End If
				With p
					.S1011 = txtS1011.Text.Trim
					.S1021 = txtS1021.Text.Trim
					.LanguageId = Session("LanguageId")
					.MSDSId = hidMSDSID.Value
				End With
				If _isNew Then
					p.Created = Now
					p.Creator = Session("UserName")
					data.MSDSSection10.AddObject(p)
				Else
					p.Modified = Now
					p.Modifier = Session("UserName")
				End If
				data.SaveChanges()

				Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section10:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
				Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section11.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)

			Catch ex As Exception
				log4net.Config.XmlConfigurator.Configure()
				log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
				Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
			Finally
				GC.Collect()
			End Try
		End Using
	End Sub
#End Region
#Region "PRIAVTE SUB AND FUNCTION"
	Sub CheckData()
		Using data As New VSEntities
			Dim LanguageId = Session("LanguageId").ToString()
			Dim p = (From q In data.MSDSSection10
					 Where q.LanguageId = LanguageId And q.MSDSId = hidMSDSID.Value
					 ).FirstOrDefault()
			If Not p Is Nothing Then
				txtS1011.Text = p.S1011
				txtS1021.Text = p.S1021
				hidID.Value = p.Section10Id
            End If

        
		End Using
	End Sub
#End Region
End Class
